import xml.etree.ElementTree as ET
import numpy as np


# 尺寸
class Size(object):
    def __init__(self):
        self.height = 0;
        self.width = 0;


# 空间
class Area(object):
    def __init__(self):
        self.index = [];
        self.location = [];


# 障碍物
class Block(object):
    def __init__(self):
        self.uid="";
        self.type = "";
        self.start = [];
        self.end = [];
        self.size = Size();
        self.area = Area();


# 全部障碍物
class BlockBase(object):
    def __init__(self, xmlPath):
        self._blocks = [];
        self.needReadType = ["Column", "Frame", "Wall", "Floor", "Stairs"];
        readxml(self, xmlPath);


def readxml(blockBase, xmlPath):
    tree = ET.parse(xmlPath)
    root = tree.getroot()

    for child1 in root.find("Entitys"):
        if(child1.tag!="Entity"):
            continue;
            
        typeName = child1.attrib["type"];
        if(typeName not in blockBase.needReadType):
            continue;

        block = Block();
        block.type = child1.attrib["type"];
        block.uid = child1.attrib["UniqueId"];

        if(typeName=="Wall"):
            #墙
            locationEntXml=child1.find("LocationEnt");
            #
            if(locationEntXml!=None):
                if(locationEntXml.attrib["type"]=="LineEntity"):
                    #直墙
                    basePoint,direction1,direction2,direction3=readDefaultType(child1,block);
                    block.size.width = float(child1.find("Thickness").attrib["value"]);
                    block.size.height = direction3.tolist()[2];
                    # block.size.height = float(child1.find("Height").attrib["value"]);

                    #墙上移修正
                    tempArr = [];
                    [tempArr.append(float(tempStr)) for tempStr in locationEntXml.attrib["StartPoint"].split(',')];
                    tempArr[2]=tempArr[2]+block.size.height/2.0
                    block.start = tempArr;
                    
                    tempArr = [];
                    [tempArr.append(float(tempStr)) for tempStr in locationEntXml.attrib["EndPoint"].split(',')];
                    tempArr[2]=tempArr[2]+block.size.height/2.0
                    block.end = tempArr;
                    
                    blockBase._blocks.append(block);
                else:
                    #弧墙
                    # print(child1.attrib["UniqueId"]);
                    # print("Arc");
                    basePoint,direction1,direction2,direction3=readDefaultType(child1,block);
                    block.start = (basePoint + direction1 / 2 + direction2 / 2).tolist();
                    block.end = (block.start + direction3).tolist();
                    block.size.width = direction1.tolist()[0];
                    block.size.height = direction2.tolist()[1];
                    
                    blockBase._blocks.append(block);
                    
            else:
                #面墙
                # print(child1.attrib["UniqueId"]);
                # print("None");
                pass;
            
        elif(typeName=="Frame"):
            #梁
            symbolName=child1.find("SymbolName").attrib["value"];
            try:
                block.size.height,block.size.width=strConvert2HW(symbolName);
            except:
                break
            locationEntXml=child1.find("LocationEnt");
            
            try:
                #梁下移修正
                tempArr = [];
                [tempArr.append(float(tempStr) ) for tempStr in locationEntXml.attrib["StartPoint"].split(',')];
                #[tempArr.append(float(tempStr)* 304.8 ) for tempStr in locationEntXml.attrib["StartPoint"].split(',')];
                tempArr[2]=tempArr[2]-block.size.height/2
                block.start = tempArr;
                
                tempArr = [];
                [tempArr.append(float(tempStr) ) for tempStr in locationEntXml.attrib["EndPoint"].split(',')];
                # [tempArr.append(float(tempStr) * 304.8) for tempStr in locationEntXml.attrib["EndPoint"].split(',')];
                tempArr[2]=tempArr[2]-block.size.height/2
                block.end = tempArr;
                
                blockBase._blocks.append(block);
            except:
                pass
            
        else :
        #其他 柱 楼板 楼梯 梁(暂时)
        #由下往上作为长度,起点,终点  一般用于柱/楼板
        #这里的高度不是看上去的高度,而是以起点终点为长,是长的高度
            basePoint,direction1,direction2,direction3=readDefaultType(child1,block);
            block.start = (basePoint + direction1 / 2 + direction2 / 2).tolist();
            block.end = (block.start + direction3).tolist();
            block.size.width = direction1.tolist()[0];
            block.size.height = direction2.tolist()[1];
            
            blockBase._blocks.append(block);
        # if(block.size.width==0):
        #     print(child1.attrib["UniqueId"]);
        #     print(block.size.width);
        #     print(block.size.height);
        

def readDefaultType(child1, block):
    OriginalBox = child1.find('Box').attrib;

    tempArr = [];
    [tempArr.append(float(tempStr)) for tempStr in OriginalBox['BasePoint'].split(',')];
    basePoint = np.array(tempArr);

    tempBox = [];
    DIRECTION = "Direction";
    for i in range(3):
        array = [0, 0, 0];
        array[i] = float(OriginalBox[DIRECTION + str(i + 1)].split(',')[i]);
        tempBox.append(array);

    direction1 = np.array(tempBox[0]);
    direction2 = np.array(tempBox[1]);
    direction3 = np.array(tempBox[2]);

    return basePoint,direction1,direction2,direction3

def strConvert2HW(str):
    Hstr=[];
    Wstr=[];
    meanwhile=0;
    reverse=False;
    
    for i in str:
        if(i.isdigit() and meanwhile==0):
            meanwhile=1;
            Hstr.append(i);
        elif(i.isdigit() and meanwhile==1):
            Hstr.append(i);
        elif( not i.isdigit() and meanwhile==1):
            meanwhile=2;
            if(i=='×'):
                reverse=True;
        elif(i.isdigit() and meanwhile==2):
            meanwhile=3;
            Wstr.append(i);
        elif(i.isdigit() and meanwhile==3):
            Wstr.append(i);
        elif( not i.isdigit() and meanwhile==3):
            if(i=='×'):
                reverse=True;
            break;
            
    HeightStr="";
    for i in Hstr:
        HeightStr+=i;
        
    WidthStr="";
    for i in Wstr:
        WidthStr+=i;
        
    if(reverse):
        return float(WidthStr),float(HeightStr)
    else:
        return float(HeightStr),float(WidthStr)